import axios from 'axios'
import CONFIG from '../config'
import {message} from 'antd'

const config = {
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 12000 // request timeout
}
if (process.env.NODE_ENV !== 'development' ) {
  config.baseURL = CONFIG.baseURL
}
// create an axios instance
const service = axios.create(config)

// request interceptor
service.interceptors.request.use(config => {

    return config
  },
  error => {
    // console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(response => {
    const res = response.data;
    

    // if (res.code !== 100) {
    //   message.error(res.msg || 'Error')
    //   return Promise.reject(res)

    // } else {
    //   return res
    // }
    return res;

  }, error => {

    console.log(error)
  
    if(error.toString().indexOf('401') !== -1) {
      top.location = '/login'
    }
    else{
      message.error( error.msg || 'Internal Server Error')
    }
    return Promise.reject(error)
  }
)

export default service
